﻿
using Business.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Business.Common;
using System.Data;

namespace BusinessImp.ServiceImpl
{
    public class ExperimentServiceImpl : IExperimentService
    {

        private DBManager dbManager=new DBManager();



        public Experiment CreateExperiment(String code,String pumpModel, String experimentName, String person,String notes, Double ratedQ, Double ratedH, Double  ratedP,Double ratedE, Double ratedSpeed)
        {
            Experiment experiment = new Experiment();
            experiment.Code = code ;
            experiment.CreateTime = DateTime.Now;
            experiment.ExperimentName = experimentName;
            experiment.PumpModel = pumpModel;
            experiment.PumpModelCode = "";
            experiment.Owner = person;
            experiment.RatedQ = ratedQ;
            experiment.RatedP = ratedP;
            experiment.RatedH = ratedH;
            experiment.RatedE = ratedE;
            experiment.RatedSpeed = ratedSpeed;

            StringBuilder sb = new StringBuilder("INSERT INTO Experiment(code,experimentName,owner, createTime,  pumpModel, ratedQ,ratedH,ratedP,ratedE,ratedSpeed) values (");
            //sb.Append("'" + experiment.ExperimentName + "',");
            sb.Append("'" + experiment.Code + "',");
            sb.Append("'" + experiment.ExperimentName + "',");
            sb.Append("'" + experiment.Owner + "',");
            sb.Append("'" + TimeUtils.toLongValue(experiment.CreateTime) + "',");
            // sb.Append("'" + experiment.ExperimengCode + "',");
            sb.Append("'" + experiment.PumpModel + "',");
            sb.Append("'" + experiment.RatedQ + "',");
            sb.Append("'" + experiment.RatedH + "',");
            sb.Append("'" + experiment.RatedP + "',");
            sb.Append("'" + experiment.RatedE + "',");
            sb.Append("'" + experiment.RatedSpeed + "')");
            sb.Append(";select last_insert_rowid(); ");
            experiment.Id = dbManager.insertOrUpdate(sb.ToString());
            //Object result = sqliteUtils.InsertOrUpdateData(sb.ToString());
            //experiment.Id = Convert.ToInt32((long)result);

            //experiment.Id = dbManager.InsertPumpExperiment(experiment);
            return experiment;
        }



        public bool ExitExperiment(Experiment exp)
        {
            throw new NotImplementedException();
        }

        public string GenerateExperimentCode()
        {
            String code = "PUMPTEST-" + string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
            return code;
      
        }

        public int QueryCount(ExperimentQuery query)
        {
            String sql = this.getCountQuerySQL(query);
            int result = (int)dbManager.getObjectData(sql);

            return result;
        }

        private String getCountQuerySQL(ExperimentQuery query)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select count(*) from ");
            sb.Append(query.TableName);

            List<String> condition = new List<String>();
            if (query.Owner != null && query.Owner.Trim() != "")
            {
                condition.Add("owner like '%" + query.Owner.Trim() + "%' ");
            }
            if (query.PumpModel != null && query.PumpModel.Trim() != "")
            {
                condition.Add("pumpModel like '%" + query.PumpModel + "%' ");
            }
            if (query.StartTime != null)
            {
                condition.Add("createTime >= " + TimeUtils.toLongValue(query.StartTime));
            }
            if (query.EndTime != null)
            {
                condition.Add("createTime <=" + TimeUtils.toLongValue(query.EndTime));
            }
            if (condition.Count == 0)
            {
                return sb.ToString();
            }
            sb.Append(" where ");
            sb.Append("(" + condition[0] + ")");
            for (int i = 1; i < condition.Count; i++)
            {
                sb.Append(" and ");
                sb.Append("(" + condition[i] + ")");
            }
            //sb.Append(" limit " + query.PageSize + " offset " + (query.PageNo - 1));
            return sb.ToString();
        }

        private String getQuerySQL(ExperimentQuery query)
        {       
            StringBuilder sb = new StringBuilder();
            sb.Append("select "+query.QueryField+" from ");
            sb.Append(query.TableName);

            List<String> condition = new List<String>();
            if (query.Owner != null && query.Owner.Trim() != "")
            {
                condition.Add("owner like '%" + query.Owner.Trim() + "%' ");
            }
            if (query.PumpModel != null && query.PumpModel.Trim() != "")
            {
                condition.Add("pumpModel like '%" + query.PumpModel + "%' ");
            }
            if (query.StartTime != null)
            {
                condition.Add("createTime >= "+ TimeUtils.toLongValue(query.StartTime) );
            }
            if (query.EndTime != null)
            {
                condition.Add("createTime <=" + TimeUtils.toLongValue(query.EndTime));
            }
            if (condition.Count == 0)
            {
                return sb.ToString();
            }
            sb.Append(" where ");
            sb.Append("("+condition[0]+")");
            for (int i = 1; i < condition.Count; i++)
            {
                sb.Append(" and ");
                sb.Append("(" + condition[i] + ")");
            }
            sb.Append(" limit "+query.PageSize +" offset "+(query.PageNo-1)*query.PageSize);
            return sb.ToString();
        }

        public DataTable queryExperiment(ExperimentQuery query)
        {
            String sql = this.getQuerySQL(query);
            DataTable table =  dbManager.getData(sql);            
            return table;
        }


        public void RemoveExperimenttRecord(ExperimentRecord record)
        {
            String sql = "delete from ExperimentRecord where id = " + record.Id;
            dbManager.delete(sql);
        }

        public bool SaveExperiment(Experiment exp)
        {
            throw new NotImplementedException();
        }



        public bool SaveExperimentRecord(ExperimentRecord record)
        {
            String insertFields = "experimentCode,recordTime,workFlow,presureIn,presureOut,electricCurrent,voltage,workingPower,workingSpeed,temperature,workingHead,stpFlow,stpHead,stpShaftPower,stpPumpEfficiency,stpGroupEfficiency,workingStaticHead,workingDynamicHead,workingGeoHead,workingFrictionHead";
            StringBuilder sb = new StringBuilder("INSERT INTO ExperimentRecord(");
            sb.Append(insertFields);
            sb.Append(") values (");            

            sb.Append("'" + record.ExperimentCode.Trim() + "',");
            sb.Append("'" + TimeUtils.toLongValue(record.RecordTime) + "',");
            //sb.Append("'" + record.Experiment + "',");
            sb.Append("'" + record.WorkFlow + "',");
            sb.Append("'" + record.PresureIn + "',");
            sb.Append("'" + record.PresureOut + "',");
            sb.Append("'" + record.ElectricCurrent + "',");
            sb.Append("'" + record.Voltage + "',");
            sb.Append("'" + record.WorkingPower + "',");
            sb.Append("'" + record.WorkingSpeed + "',");
            sb.Append("'" + record.Temperature + "',");
            sb.Append("'" + record.WorkingHead + "',");
            sb.Append("'" + record.StpFlow + "',");
            sb.Append("'" + record.StpHead + "',");
            sb.Append("'" + record.StpShaftPower + "',");
            sb.Append("'" + record.StpPumpEfficiency + "',");
            sb.Append("'" + record.StpGroupEfficiency + "',");
            sb.Append("'" + record.WorkingStaticHead + "',");
            sb.Append("'" + record.WorkingDynamicHead + "',");
            sb.Append("'" + record.WorkingGeoHead + "',");
            sb.Append("'" + record.WorkingFrictionHead + "')");

            sb.Append(";select last_insert_rowid(); ");
            record.Id = dbManager.insertOrUpdate(sb.ToString());

            return true;
        }

        public void ClearExperimentContextRecord(Experiment exp)
        {
            String sql = "delete from ExperimentRecord where experimentCode = "+exp.Code;
            dbManager.delete(sql);
        }


        public void ClearExperimentContextRecord(String[] idsArr)
        {
            if (idsArr.Length == 0)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(idsArr[0]);
            for (int i = 1; i < idsArr.Length; i++)
            {
                sb.Append(",");
                sb.Append(idsArr[i]);
            }

            String sql = "delete from ExperimentRecord where id in(" + sb.ToString() + ")";
            dbManager.delete(sql);
        }


        public void ClearExperimentContextRecord(String ids)
        {
            String sql = "delete from ExperimentRecord where id in(" + ids +")";
            dbManager.delete(sql);
        }
    }
}
